package Utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.util.Properties;

public class DataSourceFactory {
    private static DataSource dataSource;

    public static synchronized DataSource getDataSource() {
        if (dataSource == null) {
            try {
                Properties properties = new Properties();
                InputStream in = DataSourceFactory.class.getClassLoader().getResourceAsStream("jdbc.properties");
                properties.load(in);
                dataSource = DruidDataSourceFactory.createDataSource(properties);
            } catch (Exception e) {
                throw new RuntimeException("Failed to initialize data source", e);
            }
        }
        return dataSource;
    }

    // 用于测试时替换数据源
    public static void setDataSource(DataSource newDataSource) {
        dataSource = newDataSource;
    }
} 